package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
    /*
     * //插入操作，自动填充
     * */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共自动填充[insert].....");
        log.info(metaObject.toString());
        //LocalDateTime.now()获取当前时间，注意，此处的name值必须和实体类中的相对应
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        //BaseContext.getCurrenId()获取用户id
        metaObject.setValue("createUser", BaseContext.getCurrenId());
        metaObject.setValue("updateUser", BaseContext.getCurrenId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update].....");
        log.info(metaObject.toString());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrenId());
    }
}
